import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import {formatMessage} from './intl';
import stringify from 'Shared/utils/stringify';
import {InitData, List, Part} from './DTO';
const baseUrl = '/parts/api/v1';

const resourceName = formatMessage({
    id: 'api.resourceName',
    defaultMessage: '备件销售订单类型不可提报备件清单'
});

const getListDesc = formatMessage({
    id: 'api.message.getListDesc',
    defaultMessage: '查询{resourceName}'
}, {resourceName});


const updateDesc = formatMessage({
    id: 'api.message.addDesc',
    defaultMessage: '编辑{resourceName}'
}, {resourceName});

const getPartsDesc = formatMessage({
    id: 'api.message.getPartsDesc',
    defaultMessage: '查询备件'
});

// 初始化查询
export const initQuery = () => request(`${baseUrl}/partSaleOrderTypes`, null, InitData)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery));

// 查询备件清单
export const getList = (id, queryCondition) => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/partSaleOrderTypes/${id}/noSubmitPart?${queryString}`, null, List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};

// 编辑清单
export const updateDetail = (id, data) => request(`${baseUrl}/partSaleOrderTypes/${id}`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, updateDesc));

// 查询备件信息
export const getParts = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/parts/forNoSubmitPart?${queryString}`, null, Part)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getPartsDesc));
};
